package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * 功能：语音消息
 */
@MessageTag(value = IMMessageType.CHAT_VOICE, flag = MessageTag.ISPERSISTED | MessageTag.ISCOUNTED)
public class RxVoiceMessageContent extends MessageContent {

    /**
     * 采用 AAC 格式进行编码录制的媒体内容本地路径。
     */
    private String localPath;

    /**
     * 媒体内容上传服务器后的网络地址，IMKit 中上传成功后 SDK 会为该属性赋值。
     */
    private String remoteUrl;

    /**
     * 语音消息的时长，最长为 60 秒（单位：秒）。
     */
    private int duration;

    /**
     * 扩展信息，可以放置任意的数据内容，也可以去掉此属性。
     */
    private String extra;

    public static RxVoiceMessageContent setRxVoiceMessageContent(String localPath, String remoteUrl, int duration, Map<String, Object> extra) {
        RxVoiceMessageContent mRXMessageContent = new RxVoiceMessageContent();
        mRXMessageContent.localPath = localPath;
        mRXMessageContent.remoteUrl = remoteUrl;
        mRXMessageContent.duration = duration;
        mRXMessageContent.extra = JSON.toJSONString(extra);
        return mRXMessageContent;
    }

    @Override
    public byte[] encode() {
        JSONObject object = new JSONObject();
        object.put("localPath", localPath);
        object.put("remoteUrl", remoteUrl);
        object.put("duration", duration);
        object.put("extra", extra);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RxVoiceMessageContent() {
    }

    public RxVoiceMessageContent(byte[] data) {
        super(data);
        String jsonStr = null;
        try {
            jsonStr = new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);

            setLocalPath(object.getString("localPath"));
            setRemoteUrl(object.getString("remoteUrl"));
            setDuration(object.getIntValue("duration"));
            setExtra(object.getString("extra"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, localPath);
        ParcelUtils.writeToParcel(dest, remoteUrl);
        ParcelUtils.writeToParcel(dest, duration);
        ParcelUtils.writeToParcel(dest, extra);
    }

    public static final Creator<RxVoiceMessageContent> CREATOR = new Creator<RxVoiceMessageContent>() {

        @Override
        public RxVoiceMessageContent createFromParcel(Parcel source) {
            return new RxVoiceMessageContent(source);
        }

        @Override
        public RxVoiceMessageContent[] newArray(int size) {
            return new RxVoiceMessageContent[size];
        }
    };


    public RxVoiceMessageContent(Parcel parcel) {
        localPath = ParcelUtils.readFromParcel(parcel);
        remoteUrl = ParcelUtils.readFromParcel(parcel);
        duration = ParcelUtils.readIntFromParcel(parcel);
        extra = ParcelUtils.readFromParcel(parcel);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public String getRemoteUrl() {
        return remoteUrl;
    }

    public void setRemoteUrl(String remoteUrl) {
        this.remoteUrl = remoteUrl;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public Map<String, Object> getMyExtra() {
        return JSON.parseObject(extra, Map.class);
    }

}
